/**
 * @class Ext.app.LoadQueryWindow
 * @extends Ext.panel.Window
 */
Ext.define('Ext.app.LoadQueryWindow', {
    extend: 'Ext.window.Window',
    alias: 'widget.loadQueryWindow',

    id: 'loadQueryWindow',
    height: 180,
    width: 400,
    layout: 'border',
    items: [
        {
            xtype: 'toolbar',
            region: 'north',
            items: [
                {
                    text: this.translations.get("ACTION.LOAD"),
                    handler : this.functions.get("ACTION.LOAD_QUERY")
                },
                {
                    text: this.translations.get("ACTION.CANCEL"),
                    handler : function(button, e) {
                        var wind = button.up('loadQueryWindow');
                        if(wind != null) {
                            wind.close();
                        }
                    }
                }
            ]
        }, 
        {
            region: 'center',
            xtype: 'form',
            bodyPadding: 10,
            defaults: {
                labelWidth: 100
            },
            items: [
                {
                    xtype: 'panel',
                    layout: 'hbox',
                    bodyPadding: 2,
                    border: false,
                    items: [
                        {
                            xtype: 'radio',
                            width: 100,
                            boxLabel: this.translations.get("HOME.FILE_SYSTEM"),
                            name: 'fileType',
                            inputValue: 'FILE_SYSTEM',
                            checked: true
                        },
                        {
                            xtype: 'panel',
                            border: false,
                            width: 250,
                            items: [
                                {
                                    xtype: 'filefield',
                                    width: 250,
                                    name: 'fileSystem',
                                    listeners: {
                                        change: function(field) {
                                            var panel = field.up("panel").up("panel");
                                            panel.down("[name=fileType]").setValue(true);
                                        }
                                    }
                                }
                            ]
                        },
                    ]
                },
                {
                    xtype: 'panel',
                    layout: 'hbox',
                    bodyPadding: 2,
                    border: false,
                    items: [
                        {
                            xtype: 'radio',
                            width: 100,
                            boxLabel: this.translations.get("HOME.NETIKA_SITE"),
                            name: 'fileType',
                            inputValue: 'NETIKA_SITE'
                        },
                        {
                            xtype: 'textfield',
                            width: 250,
                            name: 'netikaSite',
                            listeners: {
                                focus: function(field) {
                                    var panel = field.up("panel");
                                    panel.down("[name=fileType]").setValue(true);
                                }
                            }
                        }
                    ]
                },
                {
                    xtype: 'panel',
                    layout: 'hbox',
                    bodyPadding: 2,
                    border: false,
                    items: [
                        {
                            xtype: 'radio',
                            width: 100,
                            boxLabel: this.translations.get("HOME.FTP_SERVER"),
                            name: 'fileType',
                            inputValue: 'FTP_SERVER'
                        },
                        {
                            xtype: 'textfield',
                            width: 250,
                            name: 'ftpServer',
                            listeners: {
                                focus: function(field) {
                                    var panel = field.up("panel");
                                    panel.down("[name=fileType]").setValue(true);
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ],
    
    constructor: function (translations, functions) {
        this.translations = translations;
        this.functions = functions;
        
        this.callParent(arguments);
    },
    
    listeners: {
        beforeclose: function () {
            Ext.getBody().unmask();
        }
    },
    
    initComponent : function() {
        Ext.getBody().mask();
        
        this.title = this.translations.get("HOME.FILE_LOCATION");
        this.callParent();
    },
    
    getValue: function(selector) {
        var cmp = this.down(selector);
        return (cmp == null ? "" : cmp.getValue());
    },

    getData: function() {
        var cmp = this.down("[name=fileType]");
        var selector = null;
        if(cmp.getGroupValue() == "FILE_SYSTEM") {
            selector = "[name=fileSystem]";
        }
        else if(cmp.getGroupValue() == "NETIKA_SITE") {
            selector = "[name=netikaSite]";
        }
        else if(cmp.getGroupValue() == "FTP_SERVER") {
            selector = "[name=ftpServer]";
        }
        else {
            return null;
        }
        var value = this.getValue(selector);
        
        return value == "" ? null : {
            "fileType": cmp.getGroupValue(),
            "value": value
        };
    }
});
